//引入公共函数
let util = require('../../utils/util');

//获得请求的URL
let requestDomain = util.config('requestDomain');

//存放页面实例的变量
let that;

//页面Page函数
Page({

  //页面数据
  data:{

    storeId: '',

    tableNum: '',

    funList: [],

    chatBtnUseStatus: wx.canIUse ? wx.canIUse('button.open-type.contact') : false
    
  },

  /**
   * 页面加载时触发的函数
   */

  onLoad: function(){

    //存放页面实例
    that = this;

    //获得商户ID
    let storeId = util.globalData('storeId');

    //获取桌号
    let tableNum = util.globalData('tableNum');

    //设置到sessionForm
    that.setData({storeId: storeId, tableNum: tableNum});

    //设置标题为商户名称
    wx.setNavigationBarTitle({
        title: tableNum + '桌 | 服务列表',
    });

    that.getServiceList();

  },

  getServiceList: function() {
      let {storeId, tableNum} = that.data;

      //显示顶部加载图标
      wx.showNavigationBarLoading();

      //发起获取服务列表的请求
      wx.request({
          url: requestDomain + 'storeFun/find',
          data: {
              storeId: storeId,
              _3rdSession: util.globalData('_3rdSession')
          },
          method: 'POST',
          success: function (result) {
              wx.stopPullDownRefresh();
              if (util.checkSuccess(result) && result.data && result.data.data) {
                  wx.hideNavigationBarLoading();
                  result = result.data.data;
                  result.sort(function (a, b) {
                      return a.funSid - b.funSid;
                  });
                  let funList = {};
                  for (var funIndex in result) {
                      funList[result[funIndex].funId] = {
                          funId: result[funIndex].funId,
                          funName: result[funIndex].funName,
                          funDisabled: false
                      };
                  }
                  that.setData({ funList: funList });
              }
              else {
                  util.showError('服务列表错误', '获得的服务列表不是一个正确的数据，请重新开启小程序');
              }
          },
          fail: function () {
              wx.stopPullDownRefresh();
              util.showError('获取服务列表失败', '发起获取服务列表请求失败');
          }
      });
  },

  /**
   * 用户点击某项服务触发的函数
   */

  serviceTarget: function(e) {

    //获得服务的ID
    let funId = e.currentTarget.id;

    //获得服务列表
    let {funList} = that.data;

    let {storeId} = util.globalData('storeData');

    //判断是否存在对应的服务
    if(funList[funId]) {

      for(let funId in funList) {
        if(funList[funId].funDisabled == true) {
          wx.showModal({
            title: '服务提示',
            content: '抱歉，您距离上一次申请服务还未超过30秒，请稍后再试'
          });
          return;
        }
      }

      wx.showNavigationBarLoading();

      let funName = funList[funId].funName;

      funList[funId].funDisabled = true;

      funList[funId].funName = '发送中';

      that.setData({funList: funList});

      wx.request({

        url: requestDomain + 'storeFun/target',
        
        data: {
          storeId: storeId,
          _3rdSession: util.globalData('_3rdSession'),
          funId: funId,
          tableNum: util.globalData('tableNum')
        },

        method: 'POST',

        success: function(result) {

          if(util.checkSuccess(result) && result.data && result.data.data) {

            if(result.data.data == -1) {
              wx.showModal({
                title: '服务员未在线', 
                content: '当前服务员不在线，当服务员上线时将接收到您的服务通知'
              });
            }

            wx.hideNavigationBarLoading();

            let second = 30;

            funList[funId].funName = '30秒后可再发送';

            that.setData({funList: funList});

            let disabledFunInterval = setInterval(function() {

              second--;
              funList[funId].funName = second + '秒后可再发送';
              if(second == 0) {
                
                funList[funId].funDisabled = false;

                funList[funId].funName = funName;

                clearInterval(disabledFunInterval);

              }
              that.setData({funList: funList});

            }, 1000);
          }
          else {

            wx.hideNavigationBarLoading();
            
            funList[funId].funDisabled = false;

            funList[funId].funName = funName;

            that.setData({funList: funList});

            util.showError('发送失败', '服务申请在递交给服务员时出现问题，请重试');

          }

        },
        fail: function() {

          wx.hideNavigationBarLoading();

          funList[funId].funDisabled = false;

          funList[funId].funName = funName;

          that.setData({funList: funList});

          util.showError('发送失败', '在发送服务申请请求时失败，请重试');

        }
      });

    }
    else {

      //不存在对应服务显示错误提示
      util.showError('错误的服务', '此商户不存在此服务，请重新开启小程序');

    }
  },

  onPullDownRefresh: function() {
    that.getServiceList();
  }
});